Home:ALL Converter>Ada- attempting to remove blank spaces from a string?

Ada- attempting to remove blank spaces from a string?

Ask Time:2015-03-31T03:19:39         Author:Lareaper

Json Formatter

This question is in regards to a program I'm working on for an intro to programming class in Ada. The only packages I can use are the basic text, integer, and float packages. I'm writing a procedure right now to remove the spaces from the string and I keep getting a constraint error on the same line. This is the line:

if inputString(count) /= Character'Val(32) then

I have count set to start at 1 and then increment through a loop until the end of the string, which would in turn check each character of the string to see if it was different than the character value of a blank space (32). I was then going to include this line:

noSpace(count..count) := inputString(count..count) 

where noSpace is to be the string with no spaces. My line of thinking may be completely off and I may need to approach this from a completely different angle.

Update: I've now managed to get past the error message and successfully remove the spaces from my string. But now when I print the new string there is a random variety of characters and shaded spaces after it. I believe the problem is caused because the new string is shorter than the previous one and the last characters are different.

Author:Lareaper,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/29354445/ada-attempting-to-remove-blank-spaces-from-a-string
manuBriot :

Count should start at InputString'First, not at 1.\nIn particular, when InputString is created as a slice/substring of another string, it is very likely that its first character is not at index 1.",
2015-03-30T19:23:52
Mark :

Probably the bug is hid somewhere else.\n\nTry this:\n\nwith Ada.Text_IO;\n\nprocedure String_Remove_Space is\n\n function Count_Space( S : String ) return Natural is\n Sum_Of_Space : Natural := 0;\n begin\n for I in S'range loop\n if S(I) = ' ' then\n Sum_Of_Space := Sum_Of_Space + 1;\n end if;\n end loop;\n return Sum_Of_Space;\n end Count_Space;\n\n Input_String : String := \"Apple is on the tree\";\n Input_No : String := \"AppleIsOnTheTree\";\n\n No_Space : String(1..Input_String'Length - Count_Space(Input_String));\n\n Index : Positive := 1;\nbegin\n\n for I in Input_String'range loop\n if Input_String(I) /= ' ' then\n No_Space(Index) := Input_String(I);\n Index := Index + 1;\n end if;\n end loop;\n\n Ada.Text_IO.Put_Line(Input_String);\n Ada.Text_IO.Put_Line(No_Space);\n\nend String_Remove_Space;\n",
2015-03-30T20:13:31
Simon Wright :

If there are spaces in your input string, the valid part of the output string must be shorter. I imagine you wrote something like\n\nnoSpace : String := inputString;\n\n\nto start things off with noSpace as long as it would need to be if there were no spaces in inputString? then, as you squeeze the spaces out, you seem to be writing garbage (undefined characters) to the end of noSpace. This doesn’t matter so long as you only deal with the valid part.\n\nI tried this:\n\nwith Ada.Text_IO; use Ada.Text_IO;\nprocedure Lareaper is\n function Strip_Space (S : String) return String is\n Result : String := S;\n Current : Positive := Result'First;\n Last : Natural := Result'Last;\n begin\n loop\n exit when Current > Last; -- processed the whole string\n if Result (Current) = ' ' then\n -- slide the rest of the string back one\n Result (Current .. Last - 1) := Result (Current + 1 .. Last);\n -- which reduces the length by 1 too\n Last := Last - 1;\n else\n -- non-space character, skip\n Current := Current + 1;\n end if;\n end loop;\n -- return only the part of the result that doesn't contain spaces\n return Result (Result'First .. Last);\n end Strip_Space;\nbegin\n Put_Line ('|' & Strip_Space (\"\") & '|');\n Put_Line ('|' & Strip_Space (\" \") & '|');\n Put_Line ('|' & Strip_Space (\"a\") & '|');\n Put_Line ('|' & Strip_Space (\"ab\") & '|');\n Put_Line ('|' & Strip_Space (\" a\") & '|');\n Put_Line ('|' & Strip_Space (\"a \") & '|');\n Put_Line ('|' & Strip_Space (\"a b\") & '|');\n Put_Line ('|' & Strip_Space (\" a b \") & '|');\nend Lareaper;\n\n\nwhich outputs\n\n$ ./lareaper \n||\n||\n|a|\n|ab|\n|a|\n|a|\n|ab|\n|ab|\n",
2015-03-30T21:46:37
yy